package com.mall.order.services;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderDetailSingleResponse queryOrderDetail(OrderDetailRequest orderDetailRequest) {
        orderDetailRequest.requestCheck();

        //先根据orderId查询order_item
        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderDetailRequest.getOrderId());
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        //查询order
        Order order = orderMapper.selectByPrimaryKey(orderDetailRequest.getOrderId());

        //查询orderShip
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderDetailRequest.getOrderId());
        //给返回值赋值
        OrderDetailSingleResponse response = new OrderDetailSingleResponse();
        response.setGoodsList(orderItemDtos);
        response.setOrderStatus(order.getStatus());
        response.setOrderTotal(order.getPayment());
        response.setUserId(order.getUserId());
        response.setUserName(order.getBuyerNick());
        response.setTel(orderShipping.getReceiverPhone());
        response.setStreetName(orderShipping.getReceiverState()+"-"+orderShipping.getReceiverCity()+"-"
                        +orderShipping.getReceiverDistrict()+"-"+orderShipping.getReceiverAddress()
                );
        return response;
    }

    @Override
    public OrderListResponse queryOrderList(OrderListRequest request) {
        request.requestCheck();
        Long userId = request.getUserId();
        String sort = request.getSort();
        //分页
        PageHelper.startPage(request.getPage(), request.getSize());
        Example example = new Example(Order.class);
        //todo写排序语句
        //注意example.createCriteria中的property参数是封装数据库表的bean的成员变量的名字，不是数据库中的字段
        example.createCriteria().andEqualTo("userId", userId).andNotBetween("status",5,6);
        List<Order> orders = orderMapper.selectByExample(example);
        System.out.println("输出查询的orders详情"+orders);
        //遍历给List赋值
        List<OrderDetailInfo> orderDetailInfos = new LinkedList<>();
        if(orders.size()<1){
            OrderListResponse orderListResponse = new OrderListResponse();
            orderListResponse.setData(null);
            orderListResponse.setTotal(0L);
        }
        for (Order order : orders) {
            //将order给orderDetailInfo赋值
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

            //查询orderShippingDto，并赋值到orderDetailInfo
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);

            //查询List<OrderItemDto>，并赋值到返回参数中
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtos);
            orderDetailInfos.add(orderDetailInfo);
        }
        System.out.println("输出赋值的orderDetailInfo"+orderDetailInfos);
        //查询总的订单数
        PageInfo<OrderDetailInfo> orderDetailPageInfo = new PageInfo<>(orderDetailInfos);
        long total = orderDetailPageInfo.getTotal();
        System.out.println("总订单数"+total);
        //给返回参数赋值
        OrderListResponse orderListResponse = new OrderListResponse();
        orderListResponse.setData(orderDetailInfos);
        orderListResponse.setTotal(total);
        orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
        orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        System.out.println("输出service的orderListResponse"+orderListResponse);

        return orderListResponse;
    }
}
